package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	[SWF(backgroundColor="0x000000", frameRate="24")]
	//[SWF(width="760", height="786", frameRate="30", backgroundColor="0xFFFFFF")]
	public class test_As extends Sprite
	{

		private var boxWidth:int = 20;

		private var boxArr:Array = []; //화면풀사이즈 만큼 생성한 box모음
		private var viewBoxArr:Array = []; //현재 호면에 표시된 box모음


		private var music:Sound;

		public function test_As()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler, false, 0, true);
		}

		private function addToStageHandler($event:Event):void
		{
			$event.target.removeEventListener($event.type, arguments.callee);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			var count:int = 0;
			if (stage.stageWidth === 0 && stage.stageHeight === 0)
			{
				stage.addEventListener(Event.ENTER_FRAME, function($event:Event):void
					{
						if (stage.stageWidth > 0 || stage.stageHeight > 0)
						{
							stage.removeEventListener($event.type, arguments.callee);
							init();
						}
					//trace(++count); //IE8에서 두번 들어왔다. 26.                
					});
			}
			else
			{
				init();
			}
		}

		private function init():void
		{
			boxArr = [];
			var fullWidthCnt:int = (stage.fullScreenWidth / boxWidth) + 1;
			var fullHleightCnt:int = (stage.fullScreenHeight / boxWidth) + 1;
			var fullSumCnt:int = fullWidthCnt * fullHleightCnt;
			var tempColor:uint;
			for (var i:int = 0; i < fullSumCnt; i++)
			{
				tempColor = int(Math.random() * 16777215);

				var bitmapData:BitmapData = new BitmapData(boxWidth, boxWidth, false, tempColor);
				var bitamp:Bitmap = new Bitmap(bitmapData);
				bitamp.visible = false;
				boxArr.push(bitamp);
			}

			stage.addEventListener(Event.RESIZE, stageResizeHandler, false, 0, true);
			stage.dispatchEvent(new Event(Event.RESIZE));

			//http://help.adobe.com/ko_KR/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d17.html
			var file:URLRequest = new URLRequest("004.다비치 - 시간아 멈춰라.mp3");
			music = new Sound();
			music.load(file);
			music.play();
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler, false, 0, true);

			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler, false, 0, true);
		}

		private function doubleClickHandler($event:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			music.close();
		}

		private function stageResizeHandler($event:Event):void
		{
			for (var i:int = 0; i < boxArr.length; i++)
			{
				boxArr[i].visible = false;
			}

			var widthCnt:int = (stage.stageWidth / boxWidth) + 1;
			var heightCnt:int = (stage.stageHeight / boxWidth) + 1;
			var tempX:int = 0;
			var tempY:int = 0;
			var tempCnt:int = 0;
			viewBoxArr = [];
			for (var h:int = 0; h < heightCnt; h++)
			{
				tempX = 0;
				for (var w:int = 0; w < widthCnt; w++)
				{
					boxArr[tempCnt].x = tempX;
					boxArr[tempCnt].y = tempY;
					this.addChild(boxArr[tempCnt]);

					tempCnt++;
					tempX += boxWidth;

					viewBoxArr.push(boxArr[tempCnt]);
				}
				tempY += boxWidth;
			}
		}

		private var bytes:ByteArray = new ByteArray();

		private function onEnterFrameHandler($event:Event):void
		{
			SoundMixer.computeSpectrum(bytes, false, 0);

			var n:int = Math.abs((bytes.readFloat() * viewBoxArr.length) % 12);

			for (var i:int = 0; i < viewBoxArr.length; i++)
			{
				if (i % 12 == n)
				{
					viewBoxArr[i].visible = true;
				}
				else
				{
					viewBoxArr[i].visible = false;
				}
			}
		}
	}
}